package com.zhai.boot.controller;

import com.zhai.boot.common.ServiceResult;
import com.zhai.boot.domain.in.DealBatchSaveIn;
import com.zhai.boot.domain.in.DealDelIn;
import com.zhai.boot.domain.in.DealGridIn;
import com.zhai.boot.domain.in.DealSaveIn;
import com.zhai.boot.service.DealSer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Created by 翟凤玺
 * @package com.zhai.boot.controller
 * @project-name zhai-boot
 * @description 交易接口
 * @create-datetime 2020-5-8 09:57
 */
@Api("交易接口")
@RestController
@RequestMapping("/deal")
public class DealCtr {

    @Autowired
    DealSer dealSer;

    /**
     {
         "account": "abc123",
         "amount": "2.56",
         "remark": "西瓜",
         "serialNum": "00001"
     }
     */
    @ApiOperation(value="保存交易信息", notes="就是保存交易信息")
    @ApiImplicitParam(name = "in", dataType = "DealSaveIn", value = "保存交易信息的入参", required = true, paramType = "body")
    @PostMapping("/save")
    public ResponseEntity save(@RequestBody @Validated DealBatchSaveIn.DealSaveIn in) {
        ServiceResult result = dealSer.save(in);
        return ResponseEntity.ok().body(ServiceResult.defined(result));
    }

    /**
     {"batch": [{
             "account": "abc123",
             "amount": "3.56",
             "remark": "苹果",
             "serialNum": "00002"
         },
         {
             "account": "abc123",
             "amount": "2.56",
             "remark": "西瓜",
             "serialNum": "00003"
         }]}
     */
    @ApiOperation(value="批量保存交易信息", notes="就是批量保存交易信息")
    @ApiImplicitParam(name = "in", dataType = "DealBatchSaveIn", value = "批量保存交易信息的入参", required = true, paramType = "body")
    @PostMapping("/save/batch")
    public ResponseEntity batchSave(@RequestBody @Validated DealBatchSaveIn in) {
        ServiceResult result = dealSer.batchSave(in);
        return ResponseEntity.ok().body(ServiceResult.defined(result));
    }

    /** TODO:有BUG */
    @ApiOperation(value="删除交易信息", notes="就是删除交易信息")
    @ApiImplicitParam(name = "in", dataType = "DealDelIn", value = "删除交易信息的入参", required = true, paramType = "body")
    @PostMapping("/del")
    public ResponseEntity del(@RequestBody @Validated DealDelIn in) {
        ServiceResult result = dealSer.del(in);
        return ResponseEntity.ok().body(ServiceResult.defined(result));
    }

    @ApiOperation(value="交易分页查询", notes="就是交易分页查询")
    @ApiImplicitParam(name = "in", dataType = "DealGridIn", value = "交易分页查询的入参", required = true, paramType = "body")
    @PostMapping("/gird")
    public ResponseEntity grid(@RequestBody @Validated DealGridIn in) {
        ServiceResult result = dealSer.grid(in);
        return ResponseEntity.ok().body(ServiceResult.defined(result));
    }
}